package com.icare.common.service.autoconfig;

import com.alibaba.fastjson.JSON;
import com.icare.common.service.properties.RedissonProperties;
import com.icare.common.service.utils.RedissonUtils;
import com.icare.common.service.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述:
 * 创建者:田海波 于 2018年06月19日 11:52.
 */
@Configuration
@ConditionalOnBean(RedissonProperties.class)
public class RedissonAutoConfig {
    private static Log log = LogFactory.getLog(RedissonAutoConfig.class);

    @Autowired
    private RedissonProperties redissonProperties;

    @Bean
//    @ConditionalOnMissingBean(RedissonClient.class)
    public RedissonClient initRedissonBean(){
        Config config = null;
        System.out.println(redissonProperties.getAddress());
        String[] nodes = redissonProperties.getAddress().split(",");
        if(nodes.length  == 1){
            config = createSingleServerConfig(nodes);
        }else if(nodes.length > 1){
            if("Cluster".equalsIgnoreCase(redissonProperties.getClusterOrSentinel())){
                config = createClusterServerConfig(nodes);
            }else if("Sentinel".equalsIgnoreCase(redissonProperties.getClusterOrSentinel())){
                config = createSentinelServerConfig(nodes);
            }
        }
        RedissonClient client = Redisson.create(config);
        log.info("set RedissonClient to RedissonUtils"+ JSON.toJSONString(client.getNodesGroup()));
        RedissonUtils.setRedissonClient(client);
        return client;
    }

    /**
     * 创建单例redisson服务配置
     * @param nodes
     * @return
     */
    private Config createSingleServerConfig(String[] nodes){
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("redis://".concat(nodes[0]));
        if(redissonProperties.getDb() != null){
            singleServerConfig.setDatabase(redissonProperties.getDb());
        }
        if(StringUtils.isNotEmpty(redissonProperties.getPassword())){
            singleServerConfig.setPassword(redissonProperties.getPassword());
        }
        if(redissonProperties.getTimeout() != null){
            singleServerConfig.setTimeout(redissonProperties.getTimeout());
        }else{
            singleServerConfig.setTimeout(3000);
        }
        return config;
    }

    /**
     * 创建redisson集群配置
     * @param nodes
     * @return
     */
    private Config createClusterServerConfig(String[] nodes){
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        clusterServersConfig.addNodeAddress(nodes);
        if(StringUtils.isNotEmpty(redissonProperties.getPassword())){
            clusterServersConfig.setPassword(redissonProperties.getPassword());
        }
        if(redissonProperties.getTimeout() != null){
            clusterServersConfig.setTimeout(redissonProperties.getTimeout());
        }else{
            clusterServersConfig.setTimeout(3000);
        }
        return config;
    }

    /**
     * 创建redisson哨兵配置
     * @param nodes
     * @return
     */
    private Config createSentinelServerConfig(String[] nodes){
        Config config = new Config();
        SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
        sentinelServersConfig.addSentinelAddress(nodes);
        if(StringUtils.isNotEmpty(redissonProperties.getPassword())){
            sentinelServersConfig.setPassword(redissonProperties.getPassword());
        }
        if(redissonProperties.getTimeout() != null){
            sentinelServersConfig.setTimeout(redissonProperties.getTimeout());
        }else{
            sentinelServersConfig.setTimeout(3000);
        }
        return config;
    }
}
